package com.anysoft.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.util.SparseArray;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.anysoft.R;
import com.anysoft.bean.TaxPayerInfo;

import java.util.List;

/**
 * Created by lty on 2018/3/22.
 */

public class TaxPayerAdapter extends BaseMultiItemQuickAdapter<TaxPayerInfo, BaseViewHolder> {

    private OnViewCheckedChangeListener mOnViewCheckedChangeListener;

    private List<TaxPayerInfo> mData;
    private SparseArray<TaxPayerInfo> checkBoxArr = new SparseArray<>();

    public TaxPayerAdapter(List<TaxPayerInfo> data) {
        super(data);
        mData = data;
        addItemType(TaxPayerInfo.TAX_NO_OBTAIN, R.layout.item_tax_no_obtain);
        addItemType(TaxPayerInfo.TAX_OBTAIN, R.layout.item_tax_obtain);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final TaxPayerInfo item) {
        final int adapterPosition = helper.getAdapterPosition();
        switch (helper.getItemViewType()) {
            case TaxPayerInfo.TAX_NO_OBTAIN:
                if (mData.size() > 1) {
                    helper.getView(R.id.ll_deleate_noobtain_taxpayer).setVisibility(View.VISIBLE);
                } else {
                    if (helper.getAdapterPosition() == 1) {
                        helper.getView(R.id.ll_deleate_noobtain_taxpayer).setVisibility(View.GONE);
                    } else {
                        helper.getView(R.id.ll_deleate_noobtain_taxpayer).setVisibility(View.VISIBLE);
                    }
                }
                helper.addOnClickListener(R.id.tv_tax_noobtain_country);
                helper.addOnClickListener(R.id.ll_deleate_noobtain_taxpayer);
                TextView tv_tax_noobtain_country = helper.getView(R.id.tv_tax_noobtain_country);
                tv_tax_noobtain_country.setText(item.getTaxPayerCountry());
                helper.setOnCheckedChangeListener(R.id.sbtn_tax_noobtain, new MyOnCheckedChangeListener(helper, helper.getView(R.id.sbtn_tax_noobtain), item));
                final CheckBox checkBox1 = helper.getView(R.id.cb_tax_noobtain1);
                final CheckBox checkBox2 = helper.getView(R.id.cb_tax_noobtain2);
                checkBoxArr.put(adapterPosition, item);
                checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                            checkBox1.setChecked(true);
                            item.setTaxNoobtain1(true);
                            if (item.isTaxNoobtain2()) {
                                item.setTaxNoobtain2(false);
                                helper.getView(R.id.ll_tax_noobtain_cause_container).setVisibility(View.GONE);
                                checkBox2.setChecked(false);
                            }
                        } else {
                            checkBox1.setChecked(false);
                            item.setTaxNoobtain1(false);
                        }
                        checkBoxArr.put(adapterPosition, item);
                    }
                });
                checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                            checkBox2.setChecked(true);
                            item.setTaxNoobtain2(true);
                            helper.getView(R.id.ll_tax_noobtain_cause_container).setVisibility(View.VISIBLE);
                            if (item.isTaxNoobtain1()) {
                                checkBox1.setChecked(false);
                                item.setTaxNoobtain1(false);
                            }
                        } else {
                            checkBox2.setChecked(false);
                            item.setTaxNoobtain2(false);
                            helper.getView(R.id.ll_tax_noobtain_cause_container).setVisibility(View.GONE);
                        }
                        checkBoxArr.put(adapterPosition, item);
                    }
                });

                TaxPayerInfo taxPayerInfo = checkBoxArr.get(adapterPosition);
                if (taxPayerInfo.isTaxNoobtain1()) {
                    checkBox1.setChecked(true);
                } else {
                    checkBox1.setChecked(false);
                }
                if (taxPayerInfo.isTaxNoobtain2()) {
                    checkBox2.setChecked(true);
                } else {
                    checkBox2.setChecked(false);
                }

                final EditText editTextCause = (EditText) helper.getView(R.id.et_tax_noobtain_cause);
                if (editTextCause.getTag() instanceof TextWatcher) {
                    editTextCause.removeTextChangedListener((TextWatcher) editTextCause.getTag());
                }
                TextWatcher textWatcherCause = new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        item.setTaxCauseDesc(editTextCause.getText().toString());
                    }
                };
                editTextCause.addTextChangedListener(textWatcherCause);
                editTextCause.setText(item.getTaxCauseDesc());
                editTextCause.setTag(textWatcherCause);
                break;
            case TaxPayerInfo.TAX_OBTAIN:
                if (mData.size() > 1) {
                    helper.getView(R.id.ll_deleate_obtain_taxpayer).setVisibility(View.VISIBLE);
                } else {
                    if (helper.getAdapterPosition() == 1) {
                        helper.getView(R.id.ll_deleate_obtain_taxpayer).setVisibility(View.GONE);
                    } else {
                        helper.getView(R.id.ll_deleate_obtain_taxpayer).setVisibility(View.VISIBLE);
                    }
                }
                helper.addOnClickListener(R.id.tv_tax_obtain_country);
                helper.addOnClickListener(R.id.ll_deleate_obtain_taxpayer);
                helper.setOnCheckedChangeListener(R.id.sbtn_tax_obtain, new MyOnCheckedChangeListener(helper, helper.getView(R.id.sbtn_tax_obtain), item));

                TextView tv_tax_obtain_country = helper.getView(R.id.tv_tax_obtain_country);
                tv_tax_obtain_country.setText(item.getTaxPayerCountry());

                final EditText editTextNum = (EditText) helper.getView(R.id.et_tax_obtain_num);
                if (editTextNum.getTag() instanceof TextWatcher) {
                    editTextNum.removeTextChangedListener((TextWatcher) editTextNum.getTag());
                }
                TextWatcher textWatcherNum = new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        item.setTaxNum(editTextNum.getText().toString());
                    }
                };
                editTextNum.addTextChangedListener(textWatcherNum);
                editTextNum.setText(item.getTaxNum());
                editTextNum.setTag(textWatcherNum);
                break;
        }
    }

    //  删除数据
    public void removeData(int position) {
        mData.remove(position);
        //删除动画
        notifyItemRemoved(position);
        notifyDataSetChanged();
    }

    //  添加数据
    public void addData(int position) {
//      在list中添加数据，并通知条目加入一条
        TaxPayerInfo payerInfo = new TaxPayerInfo();
        payerInfo.setItemType(TaxPayerInfo.TAX_OBTAIN);
        payerInfo.setTaxNum("");
        payerInfo.setTaxPosition(position + "");
        mData.add(payerInfo);
        //添加动画
        notifyItemInserted(position);
        notifyDataSetChanged();
    }

    class MyOnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {

        private BaseViewHolder helper;
        private TaxPayerInfo taxPayerInfo;
        private View view;

        public MyOnCheckedChangeListener(BaseViewHolder helper, View view, TaxPayerInfo item) {
            this.helper = helper;
            this.view = view;
            this.taxPayerInfo = item;
        }

        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            if (mOnViewCheckedChangeListener != null) {
                mOnViewCheckedChangeListener.OnCheckedChange(taxPayerInfo, view, helper, isChecked);
            }
        }
    }

    public interface OnViewCheckedChangeListener {
        void OnCheckedChange(TaxPayerInfo taxPayerInf, View view, BaseViewHolder helper, boolean isChecked);
    }

    public void setOnViewCheckedChangeListener(OnViewCheckedChangeListener mOnViewCheckedChangeListener) {
        this.mOnViewCheckedChangeListener = mOnViewCheckedChangeListener;
    }
}
